<template>
    <div class="sidebar">
        <el-menu
            class="sidebar-el-menu"
            :default-active="activeIndex"
            :collapse="collapse"
            background-color="#324157"
            text-color="#bfcbd9"
            active-text-color="#20a0ff"
            unique-opened
            @select="handleSelect"
        >
            <template v-for="item in items">
                <template v-if="item.subs">
                    <el-submenu :index="item.index" :key="item.index">
                        <template slot="title">
                            <i :class="item.icon"></i>
                            <span slot="title">{{ item.title }}</span>
                        </template>
                        <template v-for="subItem in item.subs">
                            <el-submenu v-if="subItem.subs" :index="subItem.index" :key="subItem.index">
                                <template slot="title">{{ subItem.title }}</template>
                                <el-menu-item v-for="(threeItem, i) in subItem.subs" :key="i" :index="threeItem.index">{{
                                    threeItem.title
                                }}</el-menu-item>
                            </el-submenu>
                            <el-menu-item v-else :index="subItem.index" :key="subItem.index">
                                <span class="sub-menu-title">{{ subItem.title }}</span>
                            </el-menu-item>
                        </template>
                    </el-submenu>
                </template>
                <template v-else>
                    <el-menu-item :index="item.index" :key="item.index">
                        <i :class="item.icon"></i>
                        <span slot="title">{{ item.title }}</span>
                    </el-menu-item>
                </template>
            </template>
        </el-menu>
    </div>
</template>

<script>
import bus from '../common/bus';
export default {
    data() {
        return {
            collapse: false, // 确保 collapse 初始值为 false
            activeIndex: '', // 用于手动设置高亮的菜单项
            items: [
                {
                    icon: 'el-icon-lx-home',
                    index: 'dashboard',
                    title: '巡检稽查',
                    subs: [
                        {
                            icon: 'el-icon-lx-cascades',
                            index: '/UserManagement',
                            title: '巡检用户管理'
                        },
                        {
                            icon: 'el-icon-lx-cascades',
                            index: '/PatrolReporting',
                            title: '巡查上报'
                        },
                        {
                            icon: 'el-icon-lx-cascades',
                            index: '/Checkexamine',
                            title: '巡检考核'
                        },
                        {
                            icon: 'el-icon-lx-cascades',
                            index: '/ShiftSetting',
                            title: '班次设置'
                        },
                        {
                            icon: 'el-icon-lx-cascades',
                            index: '/CycleSetting',
                            title: '周期轮班设置'
                        },
                        {
                            icon: 'el-icon-lx-cascades',
                            index: '/AttenManagement',
                            title: '员工考勤管理'
                        },
                        {
                            icon: 'el-icon-lx-cascades',
                            index: '/CheckRules',
                            title: '打卡规则'
                        },
                        {
                            icon: 'el-icon-lx-cascades',
                            index: '/OrderDispatch',
                            title: '工单派遣'
                        },
                        {
                            icon: 'el-icon-lx-cascades',
                            index: '/CollectionRecord',
                            title: '催缴记录'
                        },
                        {
                            icon: 'el-icon-lx-cascades',
                            index: '/AppUpgrade',
                            title: 'app版本升级管理'
                        },
                        {
                            icon: 'el-icon-lx-cascades',
                            index: '/OperationLog',
                            title: '巡查操作日志'
                        },
                        {
                            icon: 'el-icon-lx-cascades',
                            index: '/DailyTrajectory',
                            title: '巡查日常轨迹'
                        }
                    ]
                }
            ]
        };
    },
    computed: {
        onRoutes() {
            return this.$route.path.replace('/', '');
        }
    },
    created() {
        // 设置初始高亮的菜单项
        this.activeIndex = this.onRoutes;

        // 通过 Event Bus 进行组件间通信，来折叠侧边栏
        bus.$on('collapse', (msg) => {
            this.collapse = msg;
            bus.$emit('collapse-content', msg);
        });
    },
    methods: {
        handleSelect(index) {
            this.activeIndex = index;
            this.$router.push(index);
        }
    }
};
</script>

<style scoped>
.sidebar {
    display: block;
    position: absolute;
    left: 0;
    top: 70px;
    bottom: 0;
    overflow-y: scroll;
}
.sub-menu-title {
    margin-left: 25px;
}
.el-menu .el-menu--inline {
    margin-left: 20px;
}
.sidebar::-webkit-scrollbar {
    width: 0;
}
.sidebar-el-menu:not(.el-menu--collapse) {
    width: 250px;
}
.sidebar > ul {
    height: 100%;
}
</style>
